package com.inews.business.service;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.inews.base.exception.BusinessException;
import com.inews.base.mapper.BeanMapper;
import com.inews.base.persistence.DynamicSpecifications;
import com.inews.base.persistence.SearchFilter;
import com.inews.base.persistence.SearchFilter.Operator;
import com.inews.base.security.vo.BaseUserVo;
import com.inews.base.utils.ExtDataUtil;
import com.inews.base.utils.InewsUtils;
import com.inews.base.vo.ExtData;
import com.inews.business.constants.InewsBusinessConstants;
import com.inews.business.entity.TblRetireStaff;
import com.inews.business.entity.TblSysUser;
import com.inews.business.repository.RetireStaffDao;
import com.inews.business.vo.RetireStaffVo;

@Service
@Transactional(readOnly=true)
public class RetireStaffServiceImpl implements RetireStaffService, InewsBusinessConstants{

	@Autowired
	private RetireStaffDao retireStaffDao;
	@Autowired
	private BaseService baseService;
	
	
	/**
	 * 分页查询退休人员信息.
	 *
	 * @param filterParams the filter params
	 * @param page the page
	 * @return the ext data
	 */
	public ExtData<RetireStaffVo> findRetireStaffPage(Map<String, Object> filterParams, Pageable page){
		List<RetireStaffVo> voList = new ArrayList<RetireStaffVo>();
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		filters.put("status", new SearchFilter("status", Operator.EQ, RS_STATUS_NORMAL));
		Page<TblRetireStaff> pagePoList = retireStaffDao.findAll(DynamicSpecifications.bySearchFilter(filters.values(), TblRetireStaff.class), page);
		for (TblRetireStaff po : pagePoList.getContent()) {
			RetireStaffVo vo = BeanMapper.map(po, RetireStaffVo.class);
			vo.setRetireDateFormat(InewsUtils.fomatDate(po.getRetireDate()));
			vo.setCountryPayFormat(InewsUtils.formatCurrency2String(po.getCountryPay()));
			vo.setCountryPayAddFormat(InewsUtils.formatCurrency2String(po.getCountryPayAdd()));
			vo.setAllowanceFormat(InewsUtils.formatCurrency2String(po.getAllowance()));
			Long basePay = po.getCountryPay() + po.getCountryPayAdd();
			vo.setBasePayFormat(InewsUtils.formatCurrency2String(basePay));
			Long sumPay = po.getCountryPay() + po.getCountryPayAdd() + po.getAllowance();
			vo.setSumPayFormat(InewsUtils.formatCurrency2String(sumPay));
			voList.add(vo);
		}
		ExtData<RetireStaffVo> extData = ExtDataUtil.genWithPagingList(voList, pagePoList.getTotalElements());
		return extData;
	}
	
	
	
	/**
	 * 保存退休人员信息.
	 *
	 * @param vo the vo
	 * @param user the user
	 * @return the ext data
	 */
	@Transactional(readOnly=false)
	public ExtData<RetireStaffVo> saveRetireStaff(RetireStaffVo vo, BaseUserVo user){
		String currentTime = baseService.getCurrentTime();
		TblSysUser editUser = baseService.getEditUser(user);
		TblRetireStaff po = BeanMapper.map(vo, TblRetireStaff.class);
		po.setRetireDate(InewsUtils.fomatDate2String(vo.getRetireDateFormat()));
		po.setCountryPay(InewsUtils.formatCurrency2Long(vo.getCountryPayFormat()));
		po.setCountryPayAdd(InewsUtils.formatCurrency2Long(vo.getCountryPayAddFormat()));
		po.setAllowance(InewsUtils.formatCurrency2Long(vo.getAllowanceFormat()));
		po.setStatus(RS_STATUS_NORMAL);
		po.setCreateTime(currentTime);
		po.setCreateUser(editUser);
		po.setLastUpdUser(editUser);
		po.setLastUpdDes(LAST_UPD_DES_ADD);
		po.setLastUpdTime(currentTime);
		retireStaffDao.save(po);
		return ExtDataUtil.genWithSingleData(vo);
	}
	
	
	
	/**
	 * 更新退休人员信息.
	 *
	 * @param vo the vo
	 * @param user the user
	 * @return the ext data
	 */
	@Transactional(readOnly=false)
	public ExtData<RetireStaffVo> updateRetireStaff(RetireStaffVo vo, BaseUserVo user){
		String currentTime = baseService.getCurrentTime();
		TblSysUser editUser = baseService.getEditUser(user);
		TblRetireStaff po = retireStaffDao.findOne(vo.getRetireId());
		po.setCompanyName(vo.getCompanyName());
		po.setStaffName(vo.getStaffName());
		po.setSex(vo.getSex());
		po.setIdCardNo(vo.getIdCardNo());
		po.setRetireType(vo.getRetireType());
		po.setRetireDate(InewsUtils.fomatDate2String(vo.getRetireDateFormat()));
		po.setWorkType(vo.getWorkType());
		po.setCountryPay(InewsUtils.formatCurrency2Long(vo.getCountryPayFormat()));
		po.setCountryPayAdd(InewsUtils.formatCurrency2Long(vo.getCountryPayAddFormat()));
		po.setAllowance(InewsUtils.formatCurrency2Long(vo.getAllowanceFormat()));
		po.setLastUpdUser(editUser);
		po.setLastUpdDes(LAST_UPD_DES_UPDATE);
		po.setLastUpdTime(currentTime);
		retireStaffDao.save(po);
		return ExtDataUtil.genWithSingleData(vo);
	}
	
	
	
	/**
	 * 删除退休人员信息.
	 *
	 * @param vo the vo
	 * @param user the user
	 * @return the ext data
	 */
	@Transactional(readOnly=false)
	public ExtData<RetireStaffVo> deleteRetireStaff(RetireStaffVo vo, BaseUserVo user){
		String currentTime = baseService.getCurrentTime();
		TblSysUser editUser = baseService.getEditUser(user);
		TblRetireStaff po = retireStaffDao.findOne(vo.getRetireId());
		po.setStatus(RS_STATUS_DELETE);
		po.setLastUpdUser(editUser);
		po.setLastUpdDes(LAST_UPD_DES_DELETE);
		po.setLastUpdTime(currentTime);
		retireStaffDao.save(po);
		return ExtDataUtil.genWithSingleData(vo);
	}
	
	
	/**
	 * 导入excel.
	 *
	 * @param excelFile the excel file
	 * @param user the user
	 * @return the ext data
	 */
	@Transactional(readOnly=false)
	public ExtData<String> importExcel(InputStream is, BaseUserVo user){
		List<RetireStaffVo> voList = new ArrayList<RetireStaffVo>();
		try {
			HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
			HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0); 
		    if(InewsUtils.isNotEmpty(hssfSheet)){
		    	//循环行Row(第一行为标题，跳过)   
		    	for(int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++){  
			        HSSFRow hssfRow = hssfSheet.getRow(rowNum);  
			        if(InewsUtils.isEmpty(hssfRow)){  
			          continue;  
			        }  
			        RetireStaffVo vo = new RetireStaffVo();
			        vo.setRetireId(InewsUtils.getValue(hssfRow.getCell(0)));
			        vo.setCompanyName(InewsUtils.getValue(hssfRow.getCell(1)));
			        vo.setStaffName(InewsUtils.getValue(hssfRow.getCell(2)));
			        vo.setSex(InewsUtils.getValue(hssfRow.getCell(3)));
			        vo.setIdCardNo(InewsUtils.getValue(hssfRow.getCell(4)));
			        vo.setRetireType(InewsUtils.getValue(hssfRow.getCell(5)));
			        vo.setRetireDateFormat(InewsUtils.getValue(hssfRow.getCell(6)));
			        vo.setRetireJob(InewsUtils.getValue(hssfRow.getCell(7)));
			        vo.setWorkType(InewsUtils.getValue(hssfRow.getCell(8)));
			        vo.setCountryPayFormat(InewsUtils.getValue(hssfRow.getCell(9)));
			        vo.setCountryPayAddFormat(InewsUtils.getValue(hssfRow.getCell(10)));
			        vo.setAllowanceFormat(InewsUtils.getValue(hssfRow.getCell(12)));
			        voList.add(vo);
		    	}  
		    }
		    for (RetireStaffVo vo : voList) {
				if(retireStaffDao.exists(vo.getRetireId())){
					updateRetireStaff(vo, user);
				}else{
					saveRetireStaff(vo, user);
				}
			}
		} catch (IOException e) {
			throw new BusinessException("导入失败");
		}
		return ExtDataUtil.genWithSingleData("导入成功");
	}
}
